﻿using HardcoreGame.Content.Buffs;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Sets.Acid
{
    public class ErosionTide : ModProjectile
    {
        public override string Texture { get { return "Terraria/Images/Projectile_" + ProjectileID.WaterStream; } }

        public override void SetDefaults()
        {
            Projectile.width = 18;
            Projectile.height = 18;
            Projectile.friendly = true;
            Projectile.DamageType = DamageClass.Magic;
            Projectile.alpha = 255;
            Projectile.penetrate = -1;
            Projectile.extraUpdates = 2;
        }

        public override void AI()
        {
            if (Projectile.wet && !Projectile.lavaWet)
            {
                Projectile.Kill();
            }
            Projectile.scale -= 0.008f;
            if (Projectile.scale <= 0f)
            {
                Projectile.Kill();
            }
            if (Projectile.ai[0] > 3f)
            {
                Projectile.velocity.Y = Projectile.velocity.Y + 0.2f;
                for (int num160 = 0; num160 < 1; num160++)
                {
                    for (int num161 = 0; num161 < 3; num161++)
                    {
                        float num162 = Projectile.velocity.X / 3f * num161;
                        float num163 = Projectile.velocity.Y / 3f * num161;
                        int num164 = 6;
                        Vector2 position55 = new(Projectile.position.X + num164, Projectile.position.Y + num164);
                        int width52 = Projectile.width - num164 * 2;
                        int height52 = Projectile.height - num164 * 2;
                        int num165 = Dust.NewDust(position55, width52, height52, DustID.TerraBlade, 0f, 0f, 100, default, 1.2f);
                        Main.dust[num165].noGravity = true;
                        Main.dust[num165].velocity *= 0.3f;
                        Main.dust[num165].velocity += Projectile.velocity * 0.5f;
                        Dust dust22 = Main.dust[num165];
                        dust22.position.X -= num162;
                        Dust dust23 = Main.dust[num165];
                        dust23.position.Y -= num163;
                    }
                    if (Main.rand.Next(8) == 0)
                    {
                        int num166 = 6;
                        Vector2 position56 = new(Projectile.position.X + num166, Projectile.position.Y + num166);
                        int width53 = Projectile.width - num166 * 2;
                        int height53 = Projectile.height - num166 * 2;
                        int num167 = Dust.NewDust(position56, width53, height53, DustID.TerraBlade, 0f, 0f, 100, default, 0.75f);
                        Main.dust[num167].velocity *= 0.5f;
                        Main.dust[num167].velocity += Projectile.velocity * 0.5f;
                    }
                }
            }
            else
            {
                Projectile.ai[0]++;
            }
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (Main.rand.Next(3) == 0)
            {
                target.AddBuff(ModContent.BuffType<Corrosion>(), 120, false);
            }
        }

        public override void OnHitPlayer(Player target, Player.HurtInfo info)
        {
            if (Main.rand.Next(3) == 0)
            {
                target.AddBuff(ModContent.BuffType<Corrosion>(), 120, false);
            }
        }
    }
}